
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/ioport.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/interrupt.h>
#include <linux/of_irq.h>
#include <linux/miscdevice.h>
#include <linux/sched.h>
#include <linux/input.h>
#include <linux/usb.h>
#include <linux/hid.h>

struct mouse_key {
	char name[128];										/* 输入设备名称 */
	char phys[64];										/* 输入设备物理路径 */
	struct input_dev *input_dev;						/* 输入设备句柄 */
	struct usb_device *usb_dev;							/* USB设备 */
	struct urb *usb_urb;								/* 传输数据的URB */
	
	uint8_t *data;										/* 数据缓存虚拟地址 */
	dma_addr_t data_phy_addr;							/* 数据缓存物理地址 */
};

static void usb_mouse_handler(struct urb *urb)
{
	int status;
	struct mouse_key *mouse_key = urb->context;
	signed char *data = mouse_key->data;
	struct input_dev *input_dev = mouse_key->input_dev;
	
	switch (urb->status)
	{
		case 0:			/* success */
		printk("get data: 0x%x %d %d %d\n", (signed char)data[0], (signed char)data[1], (signed char)data[2], (signed char)data[3]);
		input_report_key(input_dev, KEY_L, data[0] & 0x01);
		input_report_key(input_dev, KEY_S, data[0] & 0x02);
		input_report_key(input_dev, KEY_ENTER, data[0] & 0x04);
			
			input_sync(input_dev);
			break;
		case -ECONNRESET:	/* unlink */
		case -ENOENT:
		case -ESHUTDOWN:
			return;
		/* -EPIPE:  should clear the halt */
		default:		/* error */
			break;
	}
	
	//重新提交urb
	status = usb_submit_urb (urb, GFP_ATOMIC);
	if(status)
		printk("can't resubmit intr\r\n");
}

static int usb_mouse_key_open(struct input_dev *dev)
{
	struct mouse_key *mouse_key = input_get_drvdata(dev);
	
	//提交urb
	mouse_key->usb_urb->dev = mouse_key->usb_dev;
	if (usb_submit_urb(mouse_key->usb_urb, GFP_KERNEL))
		return -EIO;
	
	return 0;
}

static void usb_mouse_key_close(struct input_dev *dev)
{
	struct mouse_key *mouse_key = input_get_drvdata(dev);
	
	//取消urb
	usb_kill_urb(mouse_key->usb_urb);
}

static int usb_mouse_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
	int error;
	int pipe, maxp;
	struct usb_host_interface *interface;
	struct usb_endpoint_descriptor *endpoint;
	struct mouse_key *mouse_key;
	
	//获取host_interface
	interface = intf->cur_altsetting;
	//只能有一个端点
	if(interface->desc.bNumEndpoints != 1)
	{
		printk("device NO match\r\n");
		return -ENODEV;
	}
	//获取端点，并判断是否时中断输入端点
	endpoint = &interface->endpoint[0].desc;
	if (!usb_endpoint_is_int_in(endpoint))
	{
		printk("device NO match\r\n");
		return -ENODEV;
	}
	
	//分配设备句柄
	mouse_key = kzalloc(sizeof(struct mouse_key), GFP_KERNEL);
	if(!mouse_key)
	{
		printk("alloc memary failed\r\n");
		return -ENOMEM;
	}
	//绑定usbdev
	mouse_key->usb_dev = interface_to_usbdev(intf);
	//设置usb_interface的私有参数
	usb_set_intfdata(intf, mouse_key);
	
	//分配一个输入设备
	mouse_key->input_dev = devm_input_allocate_device(&intf->dev);
	if(!mouse_key->input_dev)
	{
		kfree(mouse_key);
		printk("alloc input device failed\r\n");
		return -ENOMEM;
	}
	
	//分配DMA buffer
	mouse_key->data = usb_alloc_coherent(mouse_key->usb_dev, 8, GFP_ATOMIC, &mouse_key->data_phy_addr);
	if(!mouse_key->data)
	{
		kfree(mouse_key);
		printk("alloc data buffer failed\r\n");
		return -ENOMEM;
	}
	
	//分配urb
	mouse_key->usb_urb = usb_alloc_urb(0, GFP_KERNEL);
	if(!mouse_key->usb_urb)
	{
		usb_free_coherent(mouse_key->usb_dev, 8, mouse_key->data, mouse_key->data_phy_addr);
		kfree(mouse_key);
		printk("alloc usb urb failed\r\n");
		return -ENOMEM;
	}
	
	//设置名称
	if (mouse_key->usb_dev->manufacturer)
		strlcpy(mouse_key->name, mouse_key->usb_dev->manufacturer, sizeof(mouse_key->name));
	if(mouse_key->usb_dev->product)
	{
		if (mouse_key->usb_dev->manufacturer)
			strlcat(mouse_key->name, " ", sizeof(mouse_key->name));
		strlcat(mouse_key->name, mouse_key->usb_dev->product, sizeof(mouse_key->name));
	}
	if(!strlen(mouse_key->name))
	{
		snprintf(mouse_key->name, sizeof(mouse_key->name),
			"USB HIDBP Mouse %04x:%04x",
			le16_to_cpu(mouse_key->usb_dev->descriptor.idVendor),
			le16_to_cpu(mouse_key->usb_dev->descriptor.idProduct));
	}
	
	//设置物理路径
	usb_make_path(mouse_key->usb_dev, mouse_key->phys, sizeof(mouse_key->phys));
	strlcat(mouse_key->phys, "/input0", sizeof(mouse_key->phys));
	
	//设置输入设备私有数据
	input_set_drvdata(mouse_key->input_dev, mouse_key);
	//设置输入设备名称和物理路径
	mouse_key->input_dev->name = mouse_key->name;
	mouse_key->input_dev->phys = mouse_key->phys;
//	//输入设备ID
//	usb_to_input_id(mouse_key->usb_dev, &mouse_key->input_dev->id);
	mouse_key->input_dev->dev.parent = &intf->dev;
	//设置事件类型，按键事件、重复事件
	__set_bit(EV_KEY, mouse_key->input_dev->evbit);
	__set_bit(EV_REL, mouse_key->input_dev->evbit);
	//设置事件码
	__set_bit(KEY_L, mouse_key->input_dev->keybit);
	__set_bit(KEY_S, mouse_key->input_dev->keybit);
	__set_bit(KEY_ENTER, mouse_key->input_dev->keybit);
	//设置打开和关闭函数
	mouse_key->input_dev->open = usb_mouse_key_open;
	mouse_key->input_dev->close = usb_mouse_key_close;
	
	//填充urb
	pipe = usb_rcvintpipe(mouse_key->usb_dev, endpoint->bEndpointAddress);
	maxp = usb_maxpacket(mouse_key->usb_dev, pipe, usb_pipeout(pipe));
	usb_fill_int_urb(mouse_key->usb_urb, mouse_key->usb_dev, 
			pipe, mouse_key->data,
			(maxp > 8 ? 8 : maxp), usb_mouse_handler,
			mouse_key, endpoint->bInterval);
	mouse_key->usb_urb->transfer_dma = mouse_key->data_phy_addr;
	mouse_key->usb_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
	
	//注册输入设备
	error = input_register_device(mouse_key->input_dev);
	if(error)
	{
		usb_free_urb(mouse_key->usb_urb);
		usb_free_coherent(mouse_key->usb_dev, 8, mouse_key->data, mouse_key->data_phy_addr);
		kfree(mouse_key);
		printk("register input device failed\r\n");
		return error;
	}
	
	return 0;
}

static void usb_mouse_disconnect(struct usb_interface *intf)
{
	struct mouse_key *mouse_key = usb_get_intfdata(intf);
	
	usb_set_intfdata(intf, NULL);
	if (mouse_key)
	{
		usb_kill_urb(mouse_key->usb_urb);
		input_unregister_device(mouse_key->input_dev);
		usb_free_urb(mouse_key->usb_urb);
		usb_free_coherent(mouse_key->usb_dev, 8, mouse_key->data, mouse_key->data_phy_addr);
		kfree(mouse_key);
	}
}

static struct usb_device_id usb_mouse_id_table [] = {
	{ USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT,
		USB_INTERFACE_PROTOCOL_MOUSE), .driver_info = (kernel_ulong_t)"it is a mouse", },
	{ }	/* Terminating entry */
};

static struct usb_driver usb_mouse_driver = {
	.name		= "usbmouse",
	.probe		= usb_mouse_probe,
	.disconnect	= usb_mouse_disconnect,
	.id_table	= usb_mouse_id_table,
};

static int usb_mouse_key_init(void)
{
	printk("%s\r\n", __FUNCTION__);
	return usb_register(&usb_mouse_driver);
}

static void usb_mouse_key_exit(void)
{
	printk("%s\r\n", __FUNCTION__);
	usb_deregister(&usb_mouse_driver);
}

module_init(usb_mouse_key_init);
module_exit(usb_mouse_key_exit);

MODULE_DESCRIPTION("virtual SPI bus driver");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("lf");
